package com.pomr.service.synthesize.impl;

import java.util.Date;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pomr.dao.base.BaseDao;
import com.pomr.model.synthesize.Linkman;
import com.pomr.model.sys.User;
import com.pomr.service.synthesize.LxrService;
import com.pomr.util.page.Page;
@Service
public class LxrServiceImpl implements LxrService {
	@Autowired
	@Qualifier("baseDaoImpl")
	private BaseDao baseDao;
	@Override
	public Page<List<Linkman>> searchLxrsByMid(String mid, String page, String limit, User user) throws Exception {
		String hql = "from Linkman where manufacturerId = "+mid+" and agency.id = "+user.getAgency().getId();
		Page<List<Linkman>> p = new Page<List<Linkman>>();
		p.setCunrrentPage(Integer.valueOf(page));
		p.setPageSize(Integer.valueOf(limit));
		return baseDao.showPage(hql.toString(), "select count(1) "+hql.toString(), p);
	}
	@Override
	@Transactional(rollbackFor=Exception.class)
	public boolean editLxr(Linkman lxr) throws Exception {
		String hql = "update Linkman set name='"+lxr.getName()+"',tel='"+lxr.getTel()+"',job='"+lxr.getJob()+"',remark='"+lxr.getRemark()+"' where id="+lxr.getId();
		return baseDao.editHql(hql);
	}
	
	@Override
	@Transactional(rollbackFor=Exception.class)
	public boolean addLxr(Linkman lxr, User user) throws Exception {
		lxr.setCreateTime(new Date());
		lxr.setAgency(user.getAgency());
		lxr.setUser(user);
		return baseDao.add(lxr);
	}
	
	@Override
	@Transactional(rollbackFor=Exception.class)
	public boolean deleteLxrs(String ids) throws Exception {
		String[] id = ids.split(",");
		for(int i=0; i<id.length; i++){
			String hql = "delete from Linkman where id="+id[i];
			if(!baseDao.remove(hql)){
				throw new Exception("删除联系人失败，主键:"+id[i]);
			}
		}
		return true;
	}

}
